<?php
include('../../include/functions.inc.php');
$user = unserialize($_SESSION['user']);
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
        <title>Asignacion de Estudiantes</title>
        <link href="../../bootstrap/css/bootstrap.css" rel="stylesheet" type="text/css" />
        <link href="../../css/ContentStyle.css" rel="stylesheet" type="text/css" />
        <link href="../../css/tableorderer.css" rel="stylesheet" type="text/css" />
        <link href="../../js/dialog/dialog.2.0.css" rel="stylesheet" type="text/css" />
        <script src="../../js/prototype.js" type="text/javascript"></script>
        <script src="../../js/jquery.js" type="text/javascript"></script>
        <script>jQuery.noConflict();</script>
        <script src="../../bootstrap/js/bootstrap.js"></script>
        <script src="../../js/jsvalidate.js" type="text/javascript"></script>
        <script src="../../js/TableOrderer.js" type="text/javascript"></script>
        <script src="../../js/dialog/dialog.2.0.js" type="text/javascript"></script>
        <script src="../../js/include.js" type="text/javascript"></script>
        <style type="text/css">
            input,
            textarea{
                width: 300px;
            }
            select{
                width: 314px;
            }
        </style>
        <script type="text/javascript">
            var cargos=new Hash();
            doit=function(c,d){
                d.checked=c.checked;
                txtA="mon_"+d.ID;
                txtB="por_"+d.ID;
                $(txtA).readOnly=!c.checked;
                $(txtB).readOnly=!c.checked;
                if(c.checked){
                    d.ABONO=d.TOT;
                    setCargo(d);
                    sumCargos();
                }else{
                    delCargo(d);
                }
            }

            setMonto=function(c,idObj){
                jQuery("[id='hdn_"+ idObj +"']").val("Y");
                var d=cargos.get(idObj);
                var porc="por_"+d.ID;
                if(isNaN(c.value)){
                    jQuery("[id='hdn_"+ idObj +"']").val("N");
                    alert("El abono debe ser numerico!");
                    c.className="errorField";
                    c.focus();	
                    $(porc).value=0;
                    c.value=0;
                    sumCargos();
                    return;
                }
                Dialogs.close();
                if(parseFloat(c.value)>parseFloat(d.TOT)){
                    jQuery("[id='hdn_"+ idObj +"']").val("N");
                    alert("El abono debe ser igual o menor al cargo");
                    c.className="errorField";
                    c.focus();
                    return;
                }
                
                c.className=""; 	  
                d.ABONO=c.value;
                sumCargos();

                //actualiza valor en porcentaje
                $(porc).value=100*(d.ABONO/d.TOT);
            };

            setPorc=function(c,idObj){
                jQuery("[id='hdn_"+ idObj +"']").val("Y");
                var d=cargos.get(idObj);
                var monto="mon_"+d.ID;
                if(isNaN(c.value)){
                    jQuery("[id='hdn_"+ idObj +"']").val("N");
                    alert("Porcentaje Invalido"); 
                    c.className="errorField";
                    c.focus();   
                    $(monto).value=0;
                    c.value=0;
                    sumCargos();
                    return;
                }
                var p=parseFloat(c.value)
                if(p>100){
                    jQuery("[id='hdn_"+ idObj +"']").val("N");
                    alert("El porcentaje no puede ser mayor que 100%");
                    c.className="errorField";
                    c.focus();
                    return;
                }
                
                c.className=""; 	  
                var porc=p/100;
                d.ABONO=d.TOT*porc;
                sumCargos();

                //actualiza valor en monto
 
                $(monto).value=d.ABONO;

            };

            setCargo=function(cargo){
                cargos.set(cargo.ID,cargo);
            };

            delCargo=function(cargo){
                cargos.unset(cargo.ID);
                sumCargos();
                //if( typeof(cargo.ID) != "undefined" ) {
                    txtA="mon_"+cargo.ID;
                    txtB="por_"+cargo.ID;
                //}
                txtA.className="";
                txtA.value=parseFloat(cargo.TOT);
                txtB.className="";
                txtB.value=parseFloat(100);   
            };

            sumCargos=function(){
                var monto=0;
                cargos.each(function(i){
                    if(i.value.checked){
                        monto+=parseFloat(i.value.ABONO); 
                    }
                });
                $('MONTO').value=parseFloat(monto);
            };

            datosEstud=function(){
                $('operation').value='id';
                $(document.ASIGNACION).request({onComplete:function(r){
                        res=r.responseJSON;
                        if(res.ERROR){
                            alert(res.ERROR);
                        }else{
                            $('ACCOUNT').value=res.AC;
                            loadCargos();
                        }
                        closeWait();
                    }});
            };

            loadCargos=function(){
                $('cargos').innerHTML='Cargando...';
                var a=new Ajax.Updater('cargos','payments.php',{method:'get',
                    parameters:Form.serialize($(document.ASIGNACION)),
                    onComplete:function(r){
                        $('cargos').update(r.responseText);
                    }}); 
            }

            var rsv;
            config=function(){
                $("cheque").hide();
                rsv=new RSV({
                    formID: $(document.ASIGNACION),
                    errorFieldClass: "errorField",
                    rules: ["digits_only,ID,Carnet es numerico!",
                        "required,NAME,Debe buscar un estudiante!"],
                    displayType : "display-html",
                    errorHTMLItemBullet : "&#8212; ",
                    onCompleteHandler:function() {
                        boolSubmit = false;
                        jQuery("[id*='hdn_']").each( function() {
                            if( jQuery(this).val() === "N" ) {
                                boolSubmit = true;
                            }
                        });
                        if( cargos.values().length === 0 ) {
                            Dialogs.alert('Debe seleccionar algun cargo a CANCELAR!');
                        }
                        else if( boolSubmit ) {
                            Dialogs.alert('Verifique los campos!');
                        }
                        else {
                            $('operation').value = 's';
                            $('json').value = JSON.stringify(cargos.values());							
                            $(document.ASIGNACION).submit();
                        }
                    }
                });
            };
            addOnLoad(config);
            depurarCargos=function(){
                cargos.each(function(i){
                    if(i.value.checked){
                        if(!$("text_"+i.value.ID).className.empty()){
                            delCargo(i.value);
                        }
                    }
                });
            };
            addOnLoad(function(){
                var f=new Hash({tittle:'Listado de Alumnos',width:500,height:500,url:'data.php?operation=stud',callback:selStudent});
                setList($('buscar'),f.toObject());
            });

            selStudent=function(s){
                $("ID").value=s.CARNET;
                $("AIU_CODE").value=s.AIU_CODE;
                $("NAME").value=s.NOMBRE_A+' '+s.NOMBRE_B+' '+s.APELLIDOS;	
                datosEstud();
                cargos=new Hash();
                $("MONTO").value=0.00;
            };

            showCheque=function(c){
                if(c.selectedIndex===1){
                    $("cheque").show();
                }else{
                    $("cheque").hide();
                }
            }
        </script>
    </head>
    <body>
        <div id="tabs">
            <ul class="nav nav-tabs">
                <li class="active">
                    <a>ANULACIÓN DE CARGOS</a>
                </li>
            </ul>
        </div>
        <div id="tabs-cont">
            <div id="rsvErrors"></div>
            <form id="ASIGNACION" name="ASIGNACION" method="GET" action="data.php">
                <table class=""  width="100%" border="0" cellspacing="0" cellpadding="0">
                    <tr>
                        <th width="50%" scope="col"></th>
                        <th width="50%" scope="col" valign='middle'>
                            <input type="button" name="cmdOk" id="cmdOk" class="btn btn-info" value="Continuar" onclick="rsv.validate();" />
                        </th>
                    </tr>
                    <tr>
                        <td colspan="2">
                           <br/> <table width="100%" border="0" cellspacing="0" cellpadding="0">
                                <tr>
                                    <td width="20%"> NO. CARNET</td>
                                    <td width="80%">
                                        <input name="ID" type="text" id="ID" readonly="readonly" />
                                        <input name="buscar" type="button" class="btn btn-small" id="buscar"  value="..." />
                                    </td>
                                </tr>
                                <tr>
                                    <td>AIU CODIGO</td>
                                    <td><input name="AIU_CODE" type="text" id="AIU_CODE" readonly="readonly" /></td>
                                </tr>

                                <tr>
                                    <td>NOMBRE ESTUDIANTE</td>
                                    <td>
                                        <input name="NAME" type="text" id="NAME" readonly="readonly" size="50" />
                                        <input type="hidden" name="ACCOUNT" id="ACCOUNT" />
                                        <input type="hidden" name="operation" id="operation" />
                                        <input type="hidden" name="json" id="json" />
                                    </td>
                                </tr>
                                <tr>
                                    <td>DESCUENTO TOTAL</td>
                                    <td><input name="MONTO" type="text" id="MONTO" readonly="readonly" /></td>
                                </tr>
                                <tr>
                                    <td colspan="2" id="cargos">&nbsp;</td>
                                </tr>
                                <tr>
                                    <td>RAZON ANULACION</td>
                                    <td><select name="TYPE_CANCEL" onchange="showCheque(this)"  id="TYPE_CANCEL">
                                            <option value="BECADOS" selected="selected">BECADO</option>
                                            <option value="ERROR PROCESO">ERROR</option>
                                            <option value="OTROS">OTROS</option>
                                        </select>
                                    </td>
                                </tr>
                                <tr>
                                    <td>COMENTARIOS</td>
                                    <td><textarea name="COMMENT_CANCEL" id="COMMENT_CANCEL" cols="45" rows="5"></textarea></td>
                                </tr>
                            </table>
                        </td>
                    </tr>
                </table>
                <div id="cheque"></div>
            </form>
        </div>
    </body>
</html>